package com.samoiloff.ui.list.smart.controller {
	import flash.geom.Rectangle;
	import flash.events.MouseEvent;
	import com.samoiloff.framework.controller.AbstractController;
	import com.samoiloff.ui.list.smart.interfaces.CellRenderer;
	import com.samoiloff.ui.list.smart.model.SmartListModel;
	import com.samoiloff.ui.list.smart.view.SmartListRenderers;
	import com.samoiloff.ui.list.smart.view.SmartListView;

	import flash.events.Event;

	/**
	 * @author Samoiloff
	 */
	public class SmartListRenderersController extends AbstractController {
		
		private var _vModel		: SmartListModel;
		private var _vView		: SmartListView;
		private var _renderers	: SmartListRenderers;
		
		public function SmartListRenderersController(model: SmartListModel) {
			super(model);
			
			_vModel = model;
			_vView = _vModel.view;
			_renderers = _vView.renderers;
			
			_vModel.addEventListener(SmartListModel.DATA_PROVIDER_CHANGED, onDataProviderChanged);
			_vModel.addEventListener(SmartListModel.CLONE_EVENTS_CHANGED, onCloneEventsChanged);
			//_vModel.addEventListener(SmartListModel.RENDER, onRender);
			_vModel.addEventListener(Event.RESIZE, updateMaxScroll);

			_vView.mainMc.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
		}

//		private function onRender(event : Event) : void {
//			//trace("SmartListRenderersController.onRender() : ");
//			render();
//		}

		private function onCloneEventsChanged(event : Event) : void {
			for (var i : int = 0; i < _renderers.length; i++) {
				addItemListeners(_renderers.getItemAt(i));
			}
		}
		
		private function addItemListeners(cellRenderer:CellRenderer):void {
			cellRenderer.addEventListener(MouseEvent.CLICK, onClick);
			if (_vModel.cloneEvents) {
				for (var i : int = 0; i < _vModel.cloneEvents.length; i++) {
					cellRenderer.addEventListener(_vModel.cloneEvents[i], _vView.component.dispatchEvent);
				}
			}
		}
		
		private function removeItemListeners(cellRenderer : CellRenderer) : void {
			cellRenderer.removeEventListener(MouseEvent.CLICK, onClick);
			if (_vModel.cloneEvents) {
				for (var i : int = 0; i < _vModel.cloneEvents.length; i++) {
					cellRenderer.removeEventListener(_vModel.cloneEvents[i], _vView.component.dispatchEvent);
				}
			}
		}

		private function onAddedToStage(e:Event) : void {
			render();
		}

		private function onDataProviderChanged(event:Event): void {
			render();
		}
		
		private function render() : void {
			//trace("SmartListRenderersController.render() : ");
			if (_vView.component.stage) {
				clearRenderers();
				
				if (_vModel.dataProvider) {
					createRenderers();
					
					var bounds:Rectangle = new Rectangle(0, 0, _vModel.width - _vView.scroll.width, _vModel.height);
					_vModel.layout.apply(_renderers.toArray(), bounds);
					
					updateMaxScroll();
				}
			}
		}
		
		private function clearRenderers() : void {
			for (var i : int = _renderers.length-1; i >= 0; i--) {
				var cellRenderer : CellRenderer = _renderers.getItemAt(i);
				removeItemListeners(cellRenderer);
				_renderers.removeItemAt(i);
			}
			_renderers.clear();
		}
		
		private function createRenderers() : void {
			//trace("SmartListRenderersController.createRenderers() : ");
			for (var i : int = 0; i < _vModel.dataProvider.length; i++) {
				var cellRenderer : CellRenderer = _vModel.cellRenderer.instantiate(_vModel.dataProvider[i], i);
				//trace("SmartListRenderersController.createRenderers() : " + i +":" + (cellRenderer.data ? cellRenderer.data.description : ''));
				addItemListeners(cellRenderer);
				_renderers.addItem(cellRenderer);
			}
		}
		

		private function updateMaxScroll(e:Event = null): void {
			_vView.scroll.maxScrollPosition = _vModel.layout.contentHeight - _vModel.height;
			_vView.scroll.visible = _vView.scroll.maxScrollPosition > 0;
			//trace("SmartListRenderersController.updateMaxScroll(); " + _vView.scroll.maxScrollPosition);
		}

		private function onClick(e:MouseEvent): void {
			var cellRenderer:CellRenderer = e.target as CellRenderer;
			_vModel.selectedIndex = cellRenderer.index;
		}
	}
}
